package com.example.taobao

import android.content.Context
import android.content.Intent
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageButton
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat.startActivity
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide

class searchedAdapter (val content : Context, val searchedList: List<searchedData>) : RecyclerView.Adapter<searchedAdapter.ViewHoilder>() {

    private lateinit var onItemClickListener: OnItemClickListener

    fun setOnItemClickListener(listener: OnItemClickListener) {
        this.onItemClickListener = listener

    }



    inner class ViewHoilder(view: View) : RecyclerView.ViewHolder(view){
        val s_image : ImageButton = view.findViewById(R.id.searched_picture)
        val s_name : TextView = view.findViewById(R.id.searched_name)
        val s_desc : TextView = view.findViewById(R.id.searched_desc)
        val s_price : TextView = view.findViewById(R.id.searched_price)
        val s_paynum : TextView = view.findViewById(R.id.searched_paynum)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHoilder {
        val view = LayoutInflater.from(content).inflate(R.layout.searched_item, parent, false)
        return ViewHoilder(view)
    }

    override fun onBindViewHolder(holder: ViewHoilder, position: Int) {
        val searched = searchedList[position]
        holder.s_name.text = searched.names
        holder.s_desc.text = searched.desc
        holder.s_paynum.text = searched.pay_num+ "付款"
        holder.s_price.text = searched.price + "￥"
        Glide.with(content).load(searched.src).into(holder.s_image)

        holder.s_image.setOnClickListener {
            onItemClickListener?.onItemClick(holder.itemView, position)
        }

    }

    override fun getItemCount(): Int {
        return  searchedList.size
    }

    interface OnItemClickListener{
        fun onItemClick(view: View, position: Int)
    }

}